package com.ruoyi.system.service.aggregation.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.aggregation.Order;
import com.ruoyi.system.domain.aggregation.User;
import com.ruoyi.system.mapper.aggregation.OrderMapper;
import com.ruoyi.system.mapper.aggregation.UserMapper;
import com.ruoyi.system.service.aggregation.IStatisticsService;

/**
 * 数据统计Service实现
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService 
{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 首页统计数据
     */
    @Override
    public Map<String, Object> getHomeStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        // 统计各类型用户数量
        User query = new User();
        
        // C端用户数量
        query.setType(1);
        int cUserCount = userMapper.selectUserList(query).size();
        
        // 业务员数量
        query.setType(2);
        int salesmanCount = userMapper.selectUserList(query).size();
        
        // 商家数量
        query.setType(3);
        int merchantCount = userMapper.selectUserList(query).size();
        
        // 区域代理数量
        query.setType(4);
        int regionCount = userMapper.selectUserList(query).size();

        result.put("cUserCount", cUserCount);
        result.put("salesmanCount", salesmanCount);
        result.put("merchantCount", merchantCount);
        result.put("regionCount", regionCount);

        // 订单统计
        Order orderQuery = new Order();
        int totalOrders = orderMapper.selectOrderList(orderQuery).size();
        
        orderQuery.setStatue(2); // 已支付
        int paidOrders = orderMapper.selectOrderList(orderQuery).size();

        result.put("totalOrders", totalOrders);
        result.put("paidOrders", paidOrders);

        return result;
    }

    /**
     * 贡献值统计（区域代理维度）
     */
    @Override
    public Map<String, Object> getContributionStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        // 获取所有区域代理
        User query = new User();
        query.setType(4); // 区域代理
        List<User> regions = userMapper.selectUserList(query);

        List<String> regionNames = new ArrayList<>();
        List<Integer> merchantCounts = new ArrayList<>();
        List<Integer> salesmanCounts = new ArrayList<>();

        for (User region : regions)
        {
            regionNames.add(region.getName());

            // 统计该区域的商家数量
            User merchantQuery = new User();
            merchantQuery.setType(3);
            merchantQuery.setRegionId(region.getId());
            int merchantCount = userMapper.selectUserList(merchantQuery).size();
            merchantCounts.add(merchantCount);

            // 统计该区域的业务员数量
            User salesmanQuery = new User();
            salesmanQuery.setType(2);
            salesmanQuery.setRegionId(region.getId());
            int salesmanCount = userMapper.selectUserList(salesmanQuery).size();
            salesmanCounts.add(salesmanCount);
        }

        result.put("regionNames", regionNames);
        result.put("merchantCounts", merchantCounts);
        result.put("salesmanCounts", salesmanCounts);

        return result;
    }

    /**
     * 订单统计
     */
    @Override
    public Map<String, Object> getOrderStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        Order query = new Order();
        List<Order> allOrders = orderMapper.selectOrderList(query);

        // 计算总订单金额
        long totalAmount = 0;
        long totalPayAmount = 0;
        
        for (Order order : allOrders)
        {
            if (order.getAmount() != null)
            {
                totalAmount += order.getAmount();
            }
            if (order.getPayAmount() != null)
            {
                totalPayAmount += order.getPayAmount();
            }
        }

        result.put("totalOrders", allOrders.size());
        result.put("totalAmount", totalAmount);
        result.put("totalPayAmount", totalPayAmount);

        return result;
    }

    /**
     * 分佣统计
     */
    @Override
    public Map<String, Object> getCommissionStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        // TODO: 统计各角色的分佣总额（需要从point表或account表统计）
        
        result.put("totalCommission", 0);
        result.put("merchantCommission", 0);
        result.put("salesmanCommission", 0);
        result.put("regionCommission", 0);

        return result;
    }
}

